﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace huaguwang
{
    //======================================================================
    //
    //        Copyright (C) 苏州逸仙软件    
    //        All rights reserved
    //
    //        filename :D_T_SoftWare_Exp.cs
    //        description :炒股软件资料维护相关操作
    //
    //        created by 何庆 at  2010-12-14 15:56
    //
    //======================================================================
    public class D_T_SoftWare_Exp : D_T_SoftWare
    {
        public M_Page DRToMPage(DataRow dr, int len, int classlen, int nrlen, string url, string classurl, string dtFormat)
        {
            var model = new M_Page()
            {
                id = dr["id"].Exp_Trim(),
                name = dr["title"].Exp_Trim().Exp_LeftByt(len),
                title = dr["title"].Exp_Trim(),
                url = url.Exp_Format(dr["id"]),
                date = dr["adddate"].Exp_DateTimeTryParse(),
                strdate = dr["adddate"].Exp_DateTimeTryParse().ToString(dtFormat),
                nr = dr["Contents"].Exp_Trim().Exp_RegexHTML().Exp_LeftByt(nrlen),
                click = dr["click"].Exp_Trim(),
                img1 = dr["pic"].Exp_Trim()
            };
            return model;

        }
        public enum OrderByType
        {
            date,
            hot,
            Topis
        }
        public string GetOrderByType(OrderByType e)
        {
            switch(e)
            {
                case OrderByType.date:
                    return "a.AddDate desc";
                case OrderByType.hot:
                    return "a.click desc";
                case OrderByType.Topis:
                    return "a.Topis desc ";
                default:
                    break;
            }
            return "a.id";
        }
        public List<M_Page> GetHotTop10(int len, int classlen, int nrlen, string url, string classurl, string dtFormat, int Top, OrderByType e)
        {
            var order = GetOrderByType(e);
            var strSql = " select Top " + Top + "  a.id,a.Title,a.AddDate,a.Contents,a.Click,a.Pic " +
                            " from T_SoftWare a" +
                            " where a.Flag=1 order by " + order;

            var item = MSSQLHelper.SQLConType.C1.Exp_GetList<M_Page>(strSql,
                dr => DRToMPage(dr, len, classlen, nrlen, url, classurl, dtFormat));
            return item;
        }




        /// <summary>
        /// 根据条件查询软件列表
        /// </summary>
        /// <param name="begin">跳过的记录数</param>
        /// <param name="pageSize">每页记录数</param>
        /// <param name="softName">软件名称</param>
        /// <param name="softSeo">软件seo关键字</param>
        /// <param name="queryCount">引用参数返回查询记录数</param>
        /// <returns>满足条件软件列表</returns>
        public List<M_T_SoftWare> GetSoftWareList(int begin, int pageSize, string softName, string softSeo, ref int queryCount)
        {
            string strTable = "dbo.T_SoftWare as a inner join T_AdminUser as b on a.UserID = b.id ";
            string strWhere = "a.Flag = 1 and a.Title like '%'+@SoftName+'%' and a.WebKey like '%'+@SoftSeo+'%' ";
            string strQuerycountSQL = ExpandMSSQL.GetPageSQL(strTable, strWhere);
            SqlParameter[] sqlParam = new SqlParameter[]
                  {
                        new SqlParameter("@SoftName",SqlDbType.NVarChar,50){ Value=softName},
                        new SqlParameter("@SoftSeo", SqlDbType.NVarChar,1000){ Value=softSeo}
                  };
            queryCount = MSSQLHelper.SQLConType.C1.Exp_GetSingle(strQuerycountSQL, sqlParam).Exp_IntTryParse();

            string strSQL = ExpandMSSQL.GetPageSQL(begin, pageSize, "a.*,b.RealName", strTable, "a.id", strWhere, "a.AddDate desc,a.Topis desc");
            var vResult = MSSQLHelper.SQLConType.C1.Exp_GetList<M_T_SoftWare>(strSQL, row => new M_T_SoftWare
            {
                id = row["id"].Exp_IntTryParse(),
                Title = row["Title"].Exp_Trim(),
                Contents = row["Contents"].Exp_Trim(),
                Pic = row["Pic"].Exp_Trim(),
                Source = row["Source"].Exp_Trim(),
                InnerKey = row["InnerKey"].Exp_Trim(),
                WebKey = row["WebKey"].Exp_Trim(),
                Flag = true,
                AddDate = row["AddDate"].Exp_DateTimeTryParse(),
                ColorKey = row["RealName"].Exp_Trim(),
                ColorTitle = row["ColorTitle"].Exp_Trim(),
                Topis = row["Topis"].Exp_IntTryParse(),
                Gold = row["Gold"].Exp_DecimalTryParse(),
                Integral = row["Integral"].Exp_IntTryParse(),
                Click = row["Click"].Exp_IntTryParse(),
                URL = row["URL"].Exp_Trim(),
                RemSign = row["RemSign"].Exp_BoolTryParse(),
                WebTitle = row["WebTitle"].Exp_Trim(),
                WebSummary = row["WebSummary"].Exp_Trim()
            }, sqlParam);
            return vResult;
        }
    }
}
